package com.gorkr.dp;

import org.junit.jupiter.api.Test;

/**
 * @author gorkr
 * @date 2022/09/10 17:32
 **/
public class E42LianXuZishuZuDeZuiDaHeIcof {

    public int maxSubArray(int[] nums) {

        // dp[i] 0...i 中以i为结尾的最大和 子数组
        int[] dp = new int[nums.length];

        // basecase
        dp[0] = nums[0];

        for (int i = 1; i < nums.length; i++) {
            dp[i] = Math.max(dp[i - 1] + nums[i], nums[i]);
        }
        int max = Integer.MIN_VALUE;

        for (int i = 0; i < nums.length; i++) {
            max = Math.max(max, dp[i]);
        }
        return max;

    }

    // 前缀和解法

    @Test
    public void test() {
        System.out.println(maxSubArray(new int[]{
            -2
        }));
    }


}
    